<!DOCTYPE html>





<html class="theme-next muse use-motion" lang="zh-CN">
<head>
  <meta charset="UTF-8">
<meta name="generator" content="Hexo 3.9.0">
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=2">
<meta name="theme-color" content="#222">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
  <link rel="apple-touch-icon" sizes="180x180" href="/images/apple-touch-icon-next.png?v=7.3.0">
  <link rel="icon" type="image/png" sizes="32x32" href="/images/favicon-32x32-next.png?v=7.3.0">
  <link rel="icon" type="image/png" sizes="16x16" href="/images/favicon-16x16-next.png?v=7.3.0">
  <link rel="mask-icon" href="/images/logo.svg?v=7.3.0" color="#222">

<link rel="stylesheet" href="/css/main.css?v=7.3.0">


<link rel="stylesheet" href="/lib/font-awesome/css/font-awesome.min.css?v=4.7.0">


<script id="hexo-configurations">
  var NexT = window.NexT || {};
  var CONFIG = {
    root: '/',
    scheme: 'Muse',
    version: '7.3.0',
    exturl: false,
    sidebar: {"position":"left","display":"post","offset":12,"onmobile":false},
    back2top: {"enable":true,"sidebar":false,"scrollpercent":false},
    save_scroll: false,
    copycode: {"enable":false,"show_result":false,"style":null},
    fancybox: false,
    mediumzoom: false,
    lazyload: false,
    pangu: false,
    algolia: {
      appID: '',
      apiKey: '',
      indexName: '',
      hits: {"per_page":10},
      labels: {"input_placeholder":"Search for Posts","hits_empty":"We didn't find any results for the search: ${query}","hits_stats":"${hits} results found in ${time} ms"}
    },
    localsearch: {"enable":false,"trigger":"auto","top_n_per_article":1,"unescape":false,"preload":false},
    path: '',
    motion: {"enable":true,"async":false,"transition":{"post_block":"fadeIn","post_header":"slideDownIn","post_body":"slideDownIn","coll_header":"slideLeftIn","sidebar":"slideUpIn"}},
    translation: {
      copy_button: '复制',
      copy_success: '复制成功',
      copy_failure: '复制失败'
    }
  };
</script>

  <meta name="description" content="功能说明 使用http mimetype为multipart/x-mixed-replace报文，实现在线视频（服务器推送(Server Push) ） 通过将图片保存文件实现拍照功能。 读取整个目录下的所有图片，显示图片并且可以对图片进行删除 使用markdown2，读取目录下的md文件，然后转换成html显示在网页上。">
<meta name="keywords" content="树莓派">
<meta property="og:type" content="article">
<meta property="og:title" content="使用服务器推送技术实现在线视频">
<meta property="og:url" content="http://yoursite.com/2018/05/17/使用服务器推送技术实现在线视频/index.html">
<meta property="og:site_name" content="XuQi&#39;s Blog">
<meta property="og:description" content="功能说明 使用http mimetype为multipart/x-mixed-replace报文，实现在线视频（服务器推送(Server Push) ） 通过将图片保存文件实现拍照功能。 读取整个目录下的所有图片，显示图片并且可以对图片进行删除 使用markdown2，读取目录下的md文件，然后转换成html显示在网页上。">
<meta property="og:locale" content="zh-CN">
<meta property="og:image" content="http://cl.ly/3g3C1c0U3f1p/Image%202016-02-27%20at%2012.45.53%20%E4%B8%8B%E5%8D%88.png">
<meta property="og:updated_time" content="2019-10-20T13:23:23.049Z">
<meta name="twitter:card" content="summary">
<meta name="twitter:title" content="使用服务器推送技术实现在线视频">
<meta name="twitter:description" content="功能说明 使用http mimetype为multipart/x-mixed-replace报文，实现在线视频（服务器推送(Server Push) ） 通过将图片保存文件实现拍照功能。 读取整个目录下的所有图片，显示图片并且可以对图片进行删除 使用markdown2，读取目录下的md文件，然后转换成html显示在网页上。">
<meta name="twitter:image" content="http://cl.ly/3g3C1c0U3f1p/Image%202016-02-27%20at%2012.45.53%20%E4%B8%8B%E5%8D%88.png">
  <link rel="canonical" href="http://yoursite.com/2018/05/17/使用服务器推送技术实现在线视频/">


<script id="page-configurations">
  // https://hexo.io/docs/variables.html
  CONFIG.page = {
    sidebar: "",
    isHome: false,
    isPost: true,
    isPage: false,
    isArchive: false
  };
</script>

  <title>使用服务器推送技术实现在线视频 | XuQi's Blog</title>
  








  <noscript>
  <style>
  .use-motion .motion-element,
  .use-motion .brand,
  .use-motion .menu-item,
  .sidebar-inner,
  .use-motion .post-block,
  .use-motion .pagination,
  .use-motion .comments,
  .use-motion .post-header,
  .use-motion .post-body,
  .use-motion .collection-title { opacity: initial; }

  .use-motion .logo,
  .use-motion .site-title,
  .use-motion .site-subtitle {
    opacity: initial;
    top: initial;
  }

  .use-motion .logo-line-before i { left: initial; }
  .use-motion .logo-line-after i { right: initial; }
  </style>
</noscript>

</head>

<body itemscope itemtype="http://schema.org/WebPage" lang="zh-CN">

  <div class="container sidebar-position-left">
    <div class="headband"></div>

    <header id="header" class="header" itemscope itemtype="http://schema.org/WPHeader">
      <div class="header-inner"><div class="site-brand-wrapper">
  <div class="site-meta">

    <div class="custom-logo-site-title">
      <a href="/" class="brand" rel="start">
        <span class="logo-line-before"><i></i></span>
        <span class="site-title">XuQi's Blog</span>
        <span class="logo-line-after"><i></i></span>
      </a>
    </div>
  </div>

  <div class="site-nav-toggle">
    <button aria-label="切换导航栏">
      <span class="btn-bar"></span>
      <span class="btn-bar"></span>
      <span class="btn-bar"></span>
    </button>
  </div>
</div>


<nav class="site-nav">
  
  <ul id="menu" class="menu">
      
      
      
        
        <li class="menu-item menu-item-home">
      
    

    <a href="/" rel="section"><i class="menu-item-icon fa fa-fw fa-home"></i> <br>首页</a>

  </li>
      
      
      
        
        <li class="menu-item menu-item-archives">
      
    

    <a href="/archives/" rel="section"><i class="menu-item-icon fa fa-fw fa-archive"></i> <br>归档</a>

  </li>
  </ul>

    

</nav>
</div>
    </header>

    


    <main id="main" class="main">
      <div class="main-inner">
        <div class="content-wrap">
          <div id="content" class="content page-post-detail">
            

  <div id="posts" class="posts-expand">
    

  <article class="post post-type-normal" itemscope itemtype="http://schema.org/Article">
  
  
  
  <div class="post-block">
    <link itemprop="mainEntityOfPage" href="http://yoursite.com/2018/05/17/使用服务器推送技术实现在线视频/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="name" content="XuQi">
      <meta itemprop="description" content="">
      <meta itemprop="image" content="/images/avatar.gif">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="XuQi's Blog">
    </span>
      <header class="post-header">

        
          <h1 class="post-title" itemprop="name headline">使用服务器推送技术实现在线视频

            
          </h1>
        

        <div class="post-meta">
            <span class="post-meta-item">
              <span class="post-meta-item-icon">
                <i class="fa fa-calendar-o"></i>
              </span>
              <span class="post-meta-item-text">发表于</span>

              
                
              

              <time title="创建时间：2018-05-17 19:52:00" itemprop="dateCreated datePublished" datetime="2018-05-17T19:52:00+00:00">2018-05-17</time>
            </span>
          
            

            
              <span class="post-meta-item">
                <span class="post-meta-item-icon">
                  <i class="fa fa-calendar-check-o"></i>
                </span>
                <span class="post-meta-item-text">更新于</span>
                <time title="修改时间：2019-10-20 13:23:23" itemprop="dateModified" datetime="2019-10-20T13:23:23+00:00">2019-10-20</time>
              </span>
            
          

          

        </div>
      </header>

    
    
    
    <div class="post-body" itemprop="articleBody">

      
        <h3 id="功能说明"><a href="#功能说明" class="headerlink" title="功能说明"></a>功能说明</h3><ol>
<li>使用http mimetype为multipart/x-mixed-replace报文，实现在线视频（服务器推送(Server Push) ）</li>
<li>通过将图片保存文件实现拍照功能。</li>
<li>读取整个目录下的所有图片，显示图片并且可以对图片进行删除</li>
<li>使用markdown2，读取目录下的md文件，然后转换成html显示在网页上。</li>
</ol>
<a id="more"></a>

<h3 id="目录结构"><a href="#目录结构" class="headerlink" title="目录结构"></a>目录结构</h3><p><img src="http://cl.ly/3g3C1c0U3f1p/Image%202016-02-27%20at%2012.45.53%20%E4%B8%8B%E5%8D%88.png" alt></p>
<h3 id="文件说明："><a href="#文件说明：" class="headerlink" title="文件说明："></a>文件说明：</h3><p>layout.html<br>所有页面的基类,主要包括一个导航栏</p>
<p>camera_layout.html<br>模板中包括几个功能button</p>
<p>video_push.html<br>用来显示视频</p>
<p>current_picture.html<br>显示拍照</p>
<p>app.py<br>定义各种路由</p>
<p>camera.py<br>和摄像头操作有关的类。</p>
<h3 id="关键技术说明"><a href="#关键技术说明" class="headerlink" title="关键技术说明"></a>关键技术说明</h3><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br></pre></td><td class="code"><pre><span class="line">＃取得设备编号为0的</span><br><span class="line">import cv2</span><br><span class="line">cap = cv2.VideoCapture(0)</span><br><span class="line"></span><br><span class="line">＃读取图片Frame</span><br><span class="line">ret,frame = cap.read()</span><br><span class="line"></span><br><span class="line"># 将图片解码，转换成string</span><br><span class="line">strframe = cv2.imencode(&apos;.jpg&apos;, frame)[1].tostring()</span><br><span class="line"></span><br><span class="line"># 将strframe 写入文件</span><br><span class="line">file_object = open(&apos;pystream/pic/test.jpg&apos;, &apos;w&apos;)</span><br><span class="line">file_object.write(strframe)</span><br><span class="line">file_object.close()</span><br><span class="line"></span><br><span class="line"># 将frame 转换成NumPy的数组</span><br><span class="line">in_img = image.fromarray(frame)</span><br><span class="line"></span><br><span class="line"># 对图片进行大小调整</span><br><span class="line">in_img.resize((newWidth,newHeight),image.ANTIALIAS)</span><br></pre></td></tr></table></figure>

<p>服务器推送(Server Push) 技术详解</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"># 设置图片的路径后，会重新发起一次http get 方法</span><br><span class="line">&lt;img src=&quot;&#123;&#123; url_for(&apos;video_feed&apos;) &#125;&#125;&quot; class=&quot;img-thumbnail center-block&quot;&gt;</span><br></pre></td></tr></table></figure>

<p>通过chrome可以看到Response Header为：</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line">HTTP/1.0 200 OK</span><br><span class="line">Content-Type: multipart/x-mixed-replace; boundary=frame</span><br><span class="line">Connection: close</span><br><span class="line">Server: Werkzeug/0.11.3 Python/2.7.10</span><br><span class="line">Date: Sat, 27 Feb 2016 14:10:47 GMT</span><br></pre></td></tr></table></figure>

<p>大致流程是：<br>使用telnet 测试</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line">xuqitekiMacBook-Air:pystream xuqi$ telnet</span><br><span class="line">telnet&gt; open 192.168.1.114 5000</span><br><span class="line">Trying 192.168.1.114...</span><br><span class="line">Connected to 192.168.1.114.</span><br><span class="line">Escape character is &apos;^]&apos;.</span><br><span class="line">GET /video_feed HTTP/1.0        </span><br><span class="line">Host:192.168.1.114</span><br></pre></td></tr></table></figure>

<p>Response 结果</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br></pre></td><td class="code"><pre><span class="line">HTTP/1.0 200 OK</span><br><span class="line">Content-Type: multipart/x-mixed-replace; boundary=frame</span><br><span class="line">Connection: close</span><br><span class="line">Server: Werkzeug/0.11.4 Python/2.7.9</span><br><span class="line">Date: Sun, 14 Feb 2016 02:29:33 GMT</span><br><span class="line"></span><br><span class="line">--frame</span><br><span class="line">Content-Type: image/jpeg</span><br><span class="line"></span><br><span class="line">JFIFC</span><br><span class="line"></span><br><span class="line"></span><br><span class="line"></span><br><span class="line">第一张图片的数据</span><br><span class="line">--frame</span><br><span class="line">Content-Type: image/jpeg</span><br><span class="line"></span><br><span class="line">JFIFC</span><br><span class="line"></span><br><span class="line"></span><br><span class="line">第二张图片的数据</span><br><span class="line">--frame</span><br></pre></td></tr></table></figure>

<p>实现方法</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line">@app.route(&apos;/video_feed&apos;)</span><br><span class="line">def video_feed():</span><br><span class="line">    return Response(gen(), mimetype=&apos;multipart/x-mixed-replace; boundary=frame&apos;)</span><br><span class="line"></span><br><span class="line">def gen():</span><br><span class="line">	...</span><br><span class="line">	yield (b&apos;--frame\r\n&apos;</span><br><span class="line">                   b&apos;Content-Type: image/jpeg\r\n\r\n&apos; + frame + b&apos;\r\n&apos;)</span><br></pre></td></tr></table></figure>

<h3 id="问题（不明白）"><a href="#问题（不明白）" class="headerlink" title="问题（不明白）"></a>问题（不明白）</h3><ol>
<li>Mac上Flask无法设置多个progress</li>
<li>Raspberry不能开启debug=True</li>
</ol>
<h3 id="待解决问题"><a href="#待解决问题" class="headerlink" title="待解决问题"></a>待解决问题</h3><p>1.Raspberry上会出现Error</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br></pre></td><td class="code"><pre><span class="line">  File &quot;/usr/lib/python2.7/SocketServer.py&quot;, line 295, in _handle_request_noblock</span><br><span class="line">    self.process_request(request, client_address)</span><br><span class="line">  File &quot;/usr/lib/python2.7/SocketServer.py&quot;, line 321, in process_request</span><br><span class="line">    self.finish_request(request, client_address)</span><br><span class="line">  File &quot;/usr/lib/python2.7/SocketServer.py&quot;, line 334, in finish_request</span><br><span class="line">    self.RequestHandlerClass(request, client_address, self)</span><br><span class="line">  File &quot;/usr/lib/python2.7/SocketServer.py&quot;, line 657, in __init__</span><br><span class="line">    self.finish()</span><br><span class="line">  File &quot;/usr/lib/python2.7/SocketServer.py&quot;, line 716, in finish</span><br><span class="line">    self.wfile.close()</span><br><span class="line">  File &quot;/usr/lib/python2.7/socket.py&quot;, line 279, in close</span><br><span class="line">    self.flush()</span><br><span class="line">  File &quot;/usr/lib/python2.7/socket.py&quot;, line 303, in flush</span><br><span class="line">    self._sock.sendall(view[write_offset:write_offset+buffer_size])</span><br><span class="line">error: [Errno 32] Broken pipe</span><br></pre></td></tr></table></figure>

<p>可能原因是因为是<br>如果客户端在服务器返回前，主动断开连接，则服务器端会报 [Errno 32] Broken pipe 错，并导致处理线程 crash.<br>没找到解决办法。</p>
<p>2.Raspberry 经常锁死，一直在走循环</p>

    </div>

    
    
    
        
      

      <footer class="post-footer">
          
            
          
          <div class="post-tags">
            
              <a href="/tags/树莓派/" rel="tag"># 树莓派</a>
            
          </div>
        

        

          <div class="post-nav">
            <div class="post-nav-next post-nav-item">
              
                <a href="/2018/05/17/微信后端配置/" rel="next" title="微信后端实现">
                  <i class="fa fa-chevron-left"></i> 微信后端实现
                </a>
              
            </div>

            <span class="post-nav-divider"></span>

            <div class="post-nav-prev post-nav-item">
              
                <a href="/2018/05/17/使用oss 实现树莓派摄像头采集/" rel="prev" title="使用oss 实现树莓派摄像头采集">
                  使用oss 实现树莓派摄像头采集 <i class="fa fa-chevron-right"></i>
                </a>
              
            </div>
          </div>
        
      </footer>
    
  </div>
  
  
  
  </article>

  </div>


          </div>
          

        </div>
          
  
  <div class="sidebar-toggle">
    <div class="sidebar-toggle-line-wrap">
      <span class="sidebar-toggle-line sidebar-toggle-line-first"></span>
      <span class="sidebar-toggle-line sidebar-toggle-line-middle"></span>
      <span class="sidebar-toggle-line sidebar-toggle-line-last"></span>
    </div>
  </div>

  <aside id="sidebar" class="sidebar">
    <div class="sidebar-inner">
        <ul class="sidebar-nav motion-element">
          <li class="sidebar-nav-toc sidebar-nav-active" data-target="post-toc-wrap">
            文章目录
          </li>
          <li class="sidebar-nav-overview" data-target="site-overview-wrap">
            站点概览
          </li>
        </ul>
      

      <div class="site-overview-wrap sidebar-panel">
        <div class="site-overview">

          <div class="site-author motion-element" itemprop="author" itemscope itemtype="http://schema.org/Person">
  <p class="site-author-name" itemprop="name">XuQi</p>
  <div class="site-description motion-element" itemprop="description"></div>
</div>
  <nav class="site-state motion-element">
      <div class="site-state-item site-state-posts">
        
          <a href="/archives/">
        
          <span class="site-state-item-count">44</span>
          <span class="site-state-item-name">日志</span>
        </a>
      </div>
    
      
      
      <div class="site-state-item site-state-tags">
        
        
        
          
        
          
        
          
        
          
        
          
        
          
        
          
        
          
        
          
        
          
        
          
        
          
        
          
        
          
        
          
        
          
        
          
        
          
        
          
        
          
        
          
        
          
        
          
        
          
        
          
        
          
        
          
        
          
        
          
        
          
        
        <span class="site-state-item-count">30</span>
        <span class="site-state-item-name">标签</span>
        
      </div>
    
  </nav>



        </div>
      </div>
      <!--noindex-->
        <div class="post-toc-wrap motion-element sidebar-panel sidebar-panel-active">
          <div class="post-toc">

            
            
            
            

            
              <div class="post-toc-content"><ol class="nav"><li class="nav-item nav-level-3"><a class="nav-link" href="#功能说明"><span class="nav-number">1.</span> <span class="nav-text">功能说明</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#目录结构"><span class="nav-number">2.</span> <span class="nav-text">目录结构</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#文件说明："><span class="nav-number">3.</span> <span class="nav-text">文件说明：</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#关键技术说明"><span class="nav-number">4.</span> <span class="nav-text">关键技术说明</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#问题（不明白）"><span class="nav-number">5.</span> <span class="nav-text">问题（不明白）</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#待解决问题"><span class="nav-number">6.</span> <span class="nav-text">待解决问题</span></a></li></ol></div>
            

          </div>
        </div>
      <!--/noindex-->
      

    </div>
  </aside>
  <div id="sidebar-dimmer"></div>


      </div>
    </main>

    <footer id="footer" class="footer">
      <div class="footer-inner">
        <div class="copyright">&copy; <span itemprop="copyrightYear">2019</span>
  <span class="with-love" id="animate">
    <i class="fa fa-user"></i>
  </span>
  <span class="author" itemprop="copyrightHolder">XuQi</span>
</div>
  <div class="powered-by">由 <a href="https://hexo.io" class="theme-link" rel="noopener" target="_blank">Hexo</a> 强力驱动 v3.9.0</div>
  <span class="post-meta-divider">|</span>
  <div class="theme-info">主题 – <a href="https://theme-next.org" class="theme-link" rel="noopener" target="_blank">NexT.Muse</a> v7.3.0</div>

        








        
      </div>
    </footer>
      <div class="back-to-top">
        <i class="fa fa-arrow-up"></i>
      </div>

    

  </div>

  
  <script src="/lib/jquery/index.js?v=3.4.1"></script>
  <script src="/lib/velocity/velocity.min.js?v=1.2.1"></script>
  <script src="/lib/velocity/velocity.ui.min.js?v=1.2.1"></script>

<script src="/js/utils.js?v=7.3.0"></script>
  <script src="/js/motion.js?v=7.3.0"></script>


  <script src="/js/schemes/muse.js?v=7.3.0"></script>


<script src="/js/next-boot.js?v=7.3.0"></script>




  




























  

  

  


  
  <script src="/js/scrollspy.js?v=7.3.0"></script>
<script src="/js/post-details.js?v=7.3.0"></script>


</body>
</html>
